/*
 * Author: Kishore Reddy
 * Url: http://commonlibrarynet.codeplex.com/
 * Title: CommonLibrary.NET
 * Copyright: � 2009 Kishore Reddy
 * License: LGPL License
 * LicenseUrl: http://commonlibrarynet.codeplex.com/license
 * Description: A C# based .NET 3.5 Open-Source collection of reusable components.
 * Usage: Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using ComLib.Entities;
using ComLib.LocationSupport;
using ComLib.ValidationSupport;
using ComLib.Account;
using ComLib.Data;


namespace ComLib.Web.Modules.Profiles
{
    /// <summary>
    /// Profile entity.
    /// </summary>
    public partial class Profile : ActiveRecordBaseEntity<Profile>, IEntity
    {
        /// <summary>
        /// Creates a new profile.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="first"></param>
        /// <param name="last"></param>
        /// <param name="email"></param>
        /// <param name="imageUrl"></param>
        /// <param name="isGravatarEnabled"></param>
        /// <param name="websiteUrl"></param>
        public static void Create(string username, int userId, string first, string last, string email, string about, string imageUrl, bool isGravatarEnabled, string websiteUrl)
        {
            Profile profile = new Profile()
            {
                UserId = userId,
                UserName = username,
                About = about,
                Email = email,
                FirstName = first,
                LastName = last,
                ImageUrl = imageUrl,
                IsGravatarEnabled = isGravatarEnabled,
                WebSite = websiteUrl
            };
            Create(profile);
        }


        /// <summary>
        /// Create the profile conditionally based on whether the profile exists already based on the checkField expression values.
        /// Also gets the user id from the users and applies it to the profiles before conditionally creating the profile.
        /// </summary>
        /// <param name="profiles"></param>
        /// <param name="getUserId"></param>
        /// <param name="checkFields"></param>
        public static void Create(IList<Profile> profiles, bool getUserId, params Expression<Func<Profile, object>>[] checkFields)
        {
            // Set the user id on the profile by querying the users repo w/ the profile's username.
            foreach (var profile in profiles)
            {
                if (profile.UserId <= 0 || getUserId)
                {
                    var user = User.Find(Query<User>.New().Where(u => u.UserNameLowered).Is(profile.UserName.ToLower())).First();
                    if (user != null)
                    {
                        profile.UserId = user.Id;
                    }
                }
            }
            // Now create the conditionally.         
            Create(profiles, checkFields);
        }
    }
}
